<template>
    <div class="login_container">
        <div class="login_box">
             
            <!-- 头像 -->
            <div class="avatar_box">
                <img src="../../assets/1.gif" alt="">
            </div>
            <!-- 登录表单区域 -->
             <el-form ref="loginFormRef" :rules="Loginrules" :model="loginForm" class="login_form">
                <!-- 用户名 -->
                <el-form-item prop="name">
                    <el-input   v-model="loginForm.name" type="name" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input  v-model="loginForm.password" type="password" prefix-icon="el-icon-s-goods"></el-input>
                </el-form-item>

            
                <el-form-item class="btns">
                   <el-button type="primary" @click="login">登录</el-button>
               
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>

import { setToken, setUserName } from "../../utils/auth";
import request from "../../utils/request";

export default{
  data(){
    return{
        //这是登录表单的数据绑定对象
        loginForm:{
            name:'admin',
            password:'password',
            token:'jdasbkjbdakjsbdkasjbdkasbdas'
        },
         //表单的验证规则对象
         Loginrules :{
             //验证用户名是否合法
             name:[
                { required: true, message: '请输入用户名', trigger: 'blur' },
                { min: 3, max: 5, message: '用户名长度在 3 到 5 个字符', trigger: 'blur' }
             ],
              // 验证密码是否合法
              password:[
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 3, max: 8, message: '密码长度在 3 到 8 个字符', trigger: 'blur' }
              ]
         }
    }
  },
  methods:{
    register(){
        this.$router.push("/Register");
    },
    login(){
             var userName = this.loginForm.name;
             var passWord = this.loginForm.password;
            request({
                url: "/api/user/login",
                method: "post",
                data: {
                userName,
                passWord
             }
            }).then(res => {
            if (res.status === 1) {
                 this.$message({
                    message: "账号或密码错误",
                    type: "error"
                 });
                }  
                if (res.msg === "登录成功") {
                this.$message({
                    message: "恭喜你，登录成功",
                    type: "success"
                });
               
                setToken(res.token);
                setUserName(res.userName);
                // localStorage.setItem('token',res.token)  //存起来
                 sessionStorage.setItem('token',res.token)  //存起来

                this.$router.push({path:"/admin",query:{name:res.userName}});
                }
             
               
        })
    }
  }
}
</script>  
<style lang="less" scoped>
.login_container{
    background-color:lightseagreen;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color:#fff;
    border-radius:3px;
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
    .avatar_box{
        height: 130px;
        width: 130px;
        border: 1px solid #eee;
        border-radius:50%;
        padding:10px;
        box-shadow:0 0  10px #ddd;
        position:absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img{
            width:100%;
            height: 100%;
            border-radius: 50%;
            background-color:#eee;
        }
    }
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
.btns{
    display: flex;
    justify-content:center;
}
</style>